package 动态规划;

import java.util.Arrays;
import java.util.HashMap;

public class 最长定差子序列 {
    public int longestSubsequence(int[] arr, int difference) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int res = 1;
        for (int i : arr) {
            map.put(i,map.getOrDefault(i-difference,0)+1);
            res = Math.max(res,map.get(i));
        }
        return res;
    }
    //超时
    public int longestSubsequence1(int[] arr, int difference) {
        int[] dp = new int[arr.length];
        Arrays.fill(dp,1);
        int res = 1;
        for (int i = 1; i < arr.length; i++) {
            for (int j = 0; j < i; j++) {
                if (arr[i]-arr[j]==difference){
                    dp[i] = Math.max(dp[i],1+dp[j]);
                }
            }
            res = Math.max(dp[i],res);
        }
        return res;
    }
}
